package com.shop.shoppingmall.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shop.shoppingmall.dao.ShUserDao;
import com.shop.shoppingmall.entity.ShHistory;
import com.shop.shoppingmall.entity.ShUser;
import com.shop.shoppingmall.service.ShHistoryService;
import com.shop.shoppingmall.util.CommonConstant;
import com.shop.shoppingmall.util.JwtUtil;
import com.shop.shoppingmall.util.api.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * (ShHistory)表控制层
 *
 * @author makejava
 * @since 2020-09-10 15:20:01
 */
@RestController
@RequestMapping("shHistory")
@CrossOrigin
@Slf4j
public class ShHistoryController {
    /**
     * 服务对象
     */
    @Resource
    private ShHistoryService shHistoryService;
    @Resource
    private ShUserDao shUserDao;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ShHistory selectOne(String id) {
        return this.shHistoryService.queryById(id);
    }

    /**
     * 添加浏览记录
     * @param jsonObject
     * @return
     */
    @PostMapping("insertHistory")
    public Result insertHistory(HttpServletRequest request,@RequestBody JSONObject jsonObject) {
        ShHistory shHistory = JSON.parseObject(jsonObject.toJSONString(), ShHistory.class);
        String name1 = JwtUtil.getUserNameByToken(request);
        ShUser shUser = shUserDao.selectLoginName(name1);
        if(shUser == null || name1 == null){
            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
        }
        log.info("用户名"+shUser.getLoginName()+"用户id"+shUser.getId());
        shHistory.setUserId(shUser.getId());
        return shHistoryService.insert(shHistory);

    }

    /**
     * 删除单条浏览记录
     * @param id
     * @return
     */
    @GetMapping("deleteHistory")
    public Result deleteHistory(@RequestParam("historyId") String id) {
        return shHistoryService.deleteById(id);
    }

    /**
     * 清空浏览记录
     * @param request
     * @return
     */
    @GetMapping("deleteByUserId")
    public Result deleteByUserId(HttpServletRequest request) {
        String name = JwtUtil.getUserNameByToken(request);
        ShUser shUser = shUserDao.selectLoginName(name);
        if(shUser == null || name == null){
            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
        }
        return shHistoryService.deleteByUserId(shUser.getId());
    }

    @GetMapping("queryAll")
    public Result queryAll(HttpServletRequest request) {
        String name = JwtUtil.getUserNameByToken(request);
        ShUser shUser = shUserDao.selectLoginName(name);
        if(shUser == null || name == null){
            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
        }
        return shHistoryService.queryAll(shUser.getId());
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @GetMapping("/deleteHistoryByAll")
    public Result deleteHistory(@RequestParam("ids") List<String> ids)
    {
        System.out.println(ids);
        return shHistoryService.deleteHistory(ids);
    }
}